package com.tools.web.ali;

import com.tools.common.io.CharsetEnum;
import com.tools.common.object.Note;
import com.tools.common.object.string.StrKit;

import java.util.Map;
import java.util.Objects;

/**
 * 支付宝配置信息包装类实体
 * */
@Note("支付宝配置信息包装类实体")
public class AlipaySource {

    @Note("申请通过的应用程序的 ID。需要去支付宝开发中心申请")
    private String appId;

    @Note("公钥")
    private String publicKey;

    @Note("私钥")
    private String privateKey;

    @Note("登录成功后重定向到哪个接口")
    private String redirectUri;

    @Note("有 【auth_base】 和 【auth_user】 两个值。")
    private String scope = "auth_user";

    @Note("支付宝网关 URL")
    private String getaway;

    private CharsetEnum charset = CharsetEnum.UTF_8;

    /* **********************************************************************************
     *
     *          构造器
     *
     * *********************************************************************************
     * */

    public AlipaySource() {}

    public static AlipaySource of() {
        return new AlipaySource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static AlipaySource from(Map properties) {
        AlipaySource source = new AlipaySource();
        if(properties == null || properties.isEmpty()) return source;
        source.setAppId((String) properties.get("alipay-oauth-appid"));
        source.setPublicKey((String) properties.get("alipay-oauth-public-key"));
        source.setPrivateKey((String) properties.get("alipay-oauth-private-key"));
        source.setRedirectUri((String) properties.get("alipay-oauth-callback-url"));
        source.setGetaway((String) properties.get("alipay-oauth-getaway"));
        String s = (String) properties.get("alipay-oauth-charset");
        source.setCharset(CharsetEnum.getEnumSafe(s));
        source.setScope((String) properties.get("alipay-oauth-scope"));
        return source;
    }

    /* **********************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************
     * */

    public String getAppId() {
        if(StrKit.isBlank(appId)) throw new NullPointerException("请配置正确的 appId");
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getPublicKey() {
        if(StrKit.isBlank(publicKey)) throw new NullPointerException("请配置正确的公钥");
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getPrivateKey() {
        if(StrKit.isBlank(privateKey)) throw new NullPointerException("请配置正确的公钥");
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getRedirectUri() {
        if(StrKit.isBlank(redirectUri)) throw new NullPointerException("请配置正确的重定向接口地址");
        return redirectUri;
    }

    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        if(!"auth_user".equals(scope) && !"auth_base".equals(scope)) return;
        this.scope = scope;
    }

    public String getGetaway() {
        if(StrKit.isBlank(getaway)) throw new NullPointerException("请配置正确的支付宝网关地址");
        return getaway;
    }

    public void setGetaway(String getaway) {
        this.getaway = getaway;
    }

    public CharsetEnum getCharset() {
        return charset;
    }

    public void setCharset(CharsetEnum charset) {
        if(charset == null) return;
        if(charset != CharsetEnum.UTF_8 && charset != CharsetEnum.GBK) return;
        this.charset = charset;
    }

    @Override
    public String toString() {
        return "AlipaySource{" +
                "appId='" + appId + '\'' +
                ", publicKey='" + publicKey + '\'' +
                ", privateKey='" + privateKey + '\'' +
                ", redirectUri='" + redirectUri + '\'' +
                ", scope='" + scope + '\'' +
                ", getaway='" + getaway + '\'' +
                ", charset=" + charset +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AlipaySource that = (AlipaySource) o;
        return Objects.equals(appId, that.appId) && Objects.equals(publicKey, that.publicKey) && Objects.equals(privateKey, that.privateKey) && Objects.equals(redirectUri, that.redirectUri) && Objects.equals(scope, that.scope) && Objects.equals(getaway, that.getaway) && charset == that.charset;
    }

    @Override
    public int hashCode() {
        return Objects.hash(appId, publicKey, privateKey, redirectUri, scope, getaway, charset);
    }


    /* **********************************************************************************
     *
     *          链式调用
     *
     * *********************************************************************************
     * */

    public AlipaySource appId(String appId) {
        this.setAppId(appId);
        return this;
    }

    public AlipaySource publicKey(String publicKey) {
        this.setPublicKey(publicKey);
        return this;
    }

    public AlipaySource privateKey(String privateKey) {
        this.setPrivateKey(privateKey);
        return this;
    }

    public AlipaySource redirectUri(String redirectUri) {
        this.setRedirectUri(redirectUri);
        return this;
    }

    public AlipaySource scope(String scope) {
        this.setScope(scope);
        return this;
    }

    public AlipaySource getaway(String getaway) {
        this.setGetaway(getaway);
        return this;
    }

    public AlipaySource charset(CharsetEnum charset) {
        this.setCharset(charset);
        return this;
    }
}
